package com.jsjosem.aftersales.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.data.response.EventResponse;

import java.util.List;
import java.util.Objects;

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHolder> {

    private final Context mContext;

    private final List<EventResponse> eventList;

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


    public EventAdapter(Context mContext, List<EventResponse> eventList) {
        this.mContext = mContext;
        this.eventList = eventList;
    }

    @Override
    public EventViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.event_list_item, parent, false);
        EventViewHolder eventViewHolder = new EventViewHolder(view);
        view.setOnClickListener(v -> {
            if (Objects.nonNull(listener)) {
                listener.onItemClick(v, eventViewHolder.getAdapterPosition());
            }
        });
        return eventViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull EventViewHolder holder, int position) {
        holder.eventTypeTv.setText(eventList.get(position).getType());
        holder.eventStateTv.setText(eventList.get(position).getStatus());
        holder.customerNameTv.setText(eventList.get(position).getCustomer_name());
        holder.eventCodeTv.setText(eventList.get(position).getEvent_number());
        holder.eventAddressTv.setText(eventList.get(position).getAddress());
        String time = eventList.get(position).getCreate_time();
        time = time.replace("T", " ");
        time = time.replace(".000000Z", "");
        holder.eventDateTv.setText(time);

    }

    @Override
    public int getItemCount() {
        return eventList.size();
    }

    static class EventViewHolder extends RecyclerView.ViewHolder {
        private final TextView eventTypeTv;
        private final TextView eventStateTv;
        private final TextView customerNameTv;
        private final TextView eventCodeTv;
        private final TextView eventAddressTv;
        private final TextView eventDateTv;

        public EventViewHolder(@NonNull View itemView) {
            super(itemView);
            eventTypeTv = itemView.findViewById(R.id.event_type_tv);
            eventStateTv = itemView.findViewById(R.id.event_state);
            customerNameTv = itemView.findViewById(R.id.customer_name_value_tv);
            eventCodeTv = itemView.findViewById(R.id.event_code_value_tv);
            eventAddressTv = itemView.findViewById(R.id.event_address_value_tv);
            eventDateTv = itemView.findViewById(R.id.create_time_tv);
        }
    }
}
